package com.ctshk.rpc.order.custom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.NumberConstant;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.custom.dto.order.CustomOrderTouristVisaDetailDTO;
import com.ctshk.rpc.order.custom.dto.tourist.CustomOrderTouristVisaListDTO;
import com.ctshk.rpc.order.custom.entity.CustomizedOrderTouristVisa;
import com.ctshk.rpc.order.custom.mapper.CustomizedOrderTouristVisaMapper;
import com.ctshk.rpc.order.custom.req.order.CustomOrderTouristVisaDetailReq;
import com.ctshk.rpc.order.custom.req.order.CustomOrderTouristVisaSubmitBatchReq;
import com.ctshk.rpc.order.custom.req.order.CustomOrderTouristVisaSupplementBatchReq;
import com.ctshk.rpc.order.custom.req.tourist.CustomOrderTouristVisaListReq;
import com.ctshk.rpc.order.custom.service.ICustomizedOrderTouristVisaService;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 游客签证记录 服务实现类
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-22
 */
@DubboService
public class CustomizedOrderTouristVisaServiceImpl extends ServiceImpl<CustomizedOrderTouristVisaMapper, CustomizedOrderTouristVisa> implements ICustomizedOrderTouristVisaService {

    @Autowired
    private CustomizedOrderTouristVisaMapper orderTouristVisaMapper;

    @DubboReference
    private ISysUserService sysUserService;


    /**
     * 簽證列表
     *
     * @param req
     * @return
     */
    @Override
    public PageResponse<CustomOrderTouristVisaListDTO> list(CustomOrderTouristVisaListReq req) {
        req.setIsDeleted(0);
        IPage<CustomOrderTouristVisaListReq> pageParam = new Page<>(req.getPageNo(), req.getPageSize());
        IPage<CustomOrderTouristVisaListDTO> pageResult = orderTouristVisaMapper.selectListBySelective(pageParam, req);
        for (CustomOrderTouristVisaListDTO record : pageResult.getRecords()) {
            UserDTO submitUser = sysUserService.loadUserInfo(record.getSubmitUserId(), 1);
            if (submitUser != null){
                record.setSubmitUserName(submitUser.getCnSurname() + submitUser.getCnName());
                record.setSubmitUserDept(submitUser.getDept());
            }
            UserDTO makeUpUser = sysUserService.loadUserInfo(record.getMakeUpUserId(), 1);
            if (makeUpUser != null){
                record.setMakeUpUserName(makeUpUser.getCnSurname() + makeUpUser.getCnName());
                record.setMakeUpUserDept(makeUpUser.getDept());
            }
        }
        PageResponse<CustomOrderTouristVisaListDTO> response = new PageResponse<>(pageResult.getRecords(),
                pageResult.getCurrent() == 1,
                pageResult.getTotal() <= (pageResult.getCurrent() * pageResult.getSize()),
                pageResult.getTotal(), pageResult.getCurrent(),
                pageResult.getSize());
        return response;
    }

    /**
     * 收齊證件提交後台
     *
     * @param req
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result submitBatch(CustomOrderTouristVisaSubmitBatchReq req) {
        List<CustomizedOrderTouristVisa> updateObjectList = new ArrayList<>();
        for (Long id : req.getTouristIdList()) {
            /*QueryWrapper<CustomizedOrderTouristVisa> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(CustomizedOrderTouristVisa::getOrderId, req.getOrderId());
            queryWrapper.lambda().eq(CustomizedOrderTouristVisa::getTouristId, touristId);
            CustomizedOrderTouristVisa dbVisa = orderTouristVisaMapper.selectOne(queryWrapper);*/
            CustomizedOrderTouristVisa dbVisa = getById(id);
            if (dbVisa == null || !Objects.equals(dbVisa.getCollectionStatus(), NumberConstant.ONE.getValue())) {
                continue;
            }
            //新增旅客签证
            CustomizedOrderTouristVisa updateObject = EntityUtil.copy(req, CustomizedOrderTouristVisa.class);
            updateObject.setId(dbVisa.getId());
            updateObject.setSubmitUserId(req.getUserId());
            updateObject.setCollectionStatus(2);
            updateObject.setSignOutStatus(3);
            updateObjectList.add(updateObject);
        }
        //批量修改
        if (updateObjectList.size() > 0){
            boolean b = updateBatchById(updateObjectList);
            if (!b){
                throw new BusinessException(SystemError.SYS_437);
            }
        }else{
            throw new BusinessException(SystemError.CUSTOM_ORDER_4022);
        }
        return Result.success();
    }

    /**
     * 提交後補資料
     *
     * @param req
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result supplementBatch(CustomOrderTouristVisaSupplementBatchReq req) {
        List<CustomizedOrderTouristVisa> updateObjectList = new ArrayList<>();
        for (Long id : req.getTouristIdList()) {
            /*QueryWrapper<CustomizedOrderTouristVisa> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(CustomizedOrderTouristVisa::getOrderId, req.getOrderId());
            queryWrapper.lambda().eq(CustomizedOrderTouristVisa::getTouristId, touristId);
            CustomizedOrderTouristVisa dbVisa = orderTouristVisaMapper.selectOne(queryWrapper);*/
            CustomizedOrderTouristVisa dbVisa = getById(id);
            if (dbVisa == null || !Objects.equals(dbVisa.getSignOutStatus(), NumberConstant.THREE.getValue()) ||
                    !Objects.equals(dbVisa.getCollectionStatus(), NumberConstant.THREE.getValue())) {
                continue;
            }
            dbVisa.setMakeUpTime(req.getMakeUpTime());
            dbVisa.setMakeUpTrackingNumber(req.getMakeUpTrackingNumber());
            dbVisa.setMakeUpUserId(req.getUserId());
            dbVisa.setStep2Remark(req.getStep2Remark());
            dbVisa.setCollectionStatus(2);
            updateObjectList.add(dbVisa);
        }
        //批量修改
        if (updateObjectList.size() > 0){
            boolean b = updateBatchById(updateObjectList);
            if (!b){
                throw new BusinessException(SystemError.SYS_437);
            }
        }else{
            throw new BusinessException(SystemError.CUSTOM_ORDER_4022);
        }
        return Result.success();
    }

    /**
     * 簽證详情
     *
     * @param req
     * @return
     */
    @Override
    public Result<CustomOrderTouristVisaDetailDTO> detail(CustomOrderTouristVisaDetailReq req) {
        CustomOrderTouristVisaDetailDTO dto = orderTouristVisaMapper.selectDetailBySelective(req);
        return Result.success(dto);
    }
}
